package se.plingapp.controllerbeans;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import se.plingapp.entitybeans.User;
import se.plingapp.enterprisebeans.UserFacade;

/**
 * User controller.
 *
 * @author Jens Ljungblad
 */
@ManagedBean
@RequestScoped
public class UserController {
    private Long userId;

    @EJB
    private UserFacade userFacade;

    private User user;

    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
        if(userId != null)
            this.setUser(userFacade.find(this.userId));
    }

    public List<User> getUserList() {
        return userFacade.findAll();
    }

    public String showCreate() {
        return "/admin/user/create.xhtml";
    }

    public String showEdit() {
        this.setUser(this.getActiveUser());
        return "/user/editprofile.xhtml";
    }

    public String showList() {
        return "/admin/user/list.xhtml";
    }
    
    public String doCreate() {
        user = userFacade.create(user);
        return "start.xhtml?faces-redirect=true";
    }

    public String doEdit() {
        User active = this.getActiveUser();
        // Hashed password and timestamps for login and login should not be updated
        this.user.setLastLogin(active.getLastLogin());
        this.user.setRegistrationDate(active.getRegistrationDate());
        this.user.setPassword(active.getPassword(), false);
        userFacade.edit(this.user);
        this.setActiveUser(user);
        return "/user/dashboard.xhtml?faces-redirect=true";
    }

    public String doEditPassword() {
        User active = this.getActiveUser();
        active.setPassword(this.user.getPassword(), false);
        userFacade.edit(active);
        return "/user/dashboard.xhtml?faces-redirect=true";
    }

    public String doDelete() {
        userFacade.remove(user);
        return this.showList();
    }

    private User getActiveUser() {
        FacesContext fc = FacesContext.getCurrentInstance();
        User u = (User)fc.getExternalContext().getSessionMap().get("user");
        return u;
    }

    private void setActiveUser(User u) {
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.getExternalContext().getSessionMap().put("user", u);
    }

    /**
     * The active user follows the user currently stored in
     * the private User object.
     */
    public String follow() {
        User active = this.getActiveUser();
        active.follow(user);
        userFacade.edit(active);
        return "/user/viewprofile.xhtml?id=" + user.getId();
    }

    /**
     * The active user unfollows the user currently stored in
     * the private User object.
     */
    public String unfollow() {
        User active = this.getActiveUser();
        active.unfollow(user);
        userFacade.edit(active);
        return "/user/viewprofile.xhtml?id=" + user.getId();
    }
}
